package com.struts2.student.action;

import java.io.File;
import java.util.List;

import com.strust2.action.BaseAction;
import com.struts2.student.entity.Classes;
import com.struts2.student.entity.Student;
import com.struts2.student.service.ClassesService;
import com.struts2.student.service.ClassesServiceImpl;
import com.struts2.student.service.StudentService;
import com.struts2.student.service.StudentServiceImpl;
import com.struts2.util.ATTSystem;
import com.struts2.util.FileUtil;

import HttpUtil.HttpUtil;

public class StudentAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	
	private Student student;
	private Integer studentId;
	private File avatarFile;
	private String avatarFileName;
	/**
	 * 查询学生信息
	 * @author Administrator
	 * @date  2017年8月22日 上午9:35:15
	 * @version v1.0
	 * @return
	 */
	public String selectStudent(){
		StudentService studentService = new StudentServiceImpl();
		List<Student> list = studentService.selectStudent();
		
		ClassesService classesService = new ClassesServiceImpl();
		List<Classes> classesList = classesService.findClasses();
		
		HttpUtil.setAttribute("list",list,"classesList",classesList);
		
		return SUCCESS;
	}
	/**
	 * 保存学生信息
	 * @author Administrator
	 * @date  2017年8月22日 上午10:18:04
	 * @version v1.0
	 * @return
	 */
	public String saveStudent(){
		if(avatarFile!=null){
			String rootPath = "/images/avatar/";
			String fileName = System.currentTimeMillis()+avatarFileName.substring(avatarFileName.lastIndexOf("."));
			FileUtil.uploadFile(avatarFile, ATTSystem.ROOT_PATH+rootPath, fileName);
			student.setAvatar(rootPath+fileName);
		}
		
		StudentService studentService = new StudentServiceImpl();
		studentService.save(student);
		return SUCCESS;
	}
	/**
	 * 根据id查找学生信息
	 * @author Administrator
	 * @date  2017年8月23日 下午3:37:58
	 * @version v1.0
	 * @return
	 */
	public String getStudentById(){
		StudentService studentService = new StudentServiceImpl();
		Student student = studentService.getStudent(studentId);
		
		ajaxMap.put("studentId", student.getStudentId());
		ajaxMap.put("realName", student.getRealName());
		ajaxMap.put("classId", student.getClasses().getClassId());
		ajaxMap.put("avatar", student.getAvatar());
		return SUCCESS;
	}
	/**
	 * 删除学生信息
	 */
	public String delStudent(){
		StudentService studentService = new StudentServiceImpl();
		studentService.delete(studentId);
		return SUCCESS;
	}
	
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	public Integer getStudentId() {
		return studentId;
	}
	public void setStudentId(Integer studentId) {
		this.studentId = studentId;
	}
	public File getAvatarFile() {
		return avatarFile;
	}
	public void setAvatarFile(File avatarFile) {
		this.avatarFile = avatarFile;
	}
	public String getAvatarFileName() {
		return avatarFileName;
	}
	public void setAvatarFileName(String avatarFileName) {
		this.avatarFileName = avatarFileName;
	}
}
